const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const Download = sequelize.define('Download', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  userId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: 'users',
      key: 'id'
    }
  },
  materialId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: 'materials',
      key: 'id'
    }
  },
  isWatermark: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: true
  },
  coinsSpent: {
    type: DataTypes.INTEGER,
    allowNull: false
  },
  downloadAt: {
    type: DataTypes.DATE,
    allowNull: false,
    defaultValue: DataTypes.NOW
  },
  ipAddress: {
    type: DataTypes.STRING(45),
    allowNull: true
  }
}, {
  tableName: 'downloads',
  indexes: [
    {
      fields: ['userId']
    },
    {
      fields: ['materialId']
    },
    {
      fields: ['downloadAt']
    }
  ]
});

module.exports = Download;